/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.incad.psh.pshassign.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Widget;
import cz.incad.psh.pshassign.client.json.ResultDoc;
import cz.incad.psh.pshassign.client.json.SearchData;
import cz.incad.psh.pshassign.client.rpc.PSHService;
import cz.incad.psh.pshassign.client.rpc.PSHServiceAsync;

/**
 *
 * @author alberto
 */
public class InvenioDocInfo extends Composite {
    
    private static InvenioDocInfoUiBinder uiBinder = GWT.create(InvenioDocInfoUiBinder.class);
    PSHServiceAsync pshService = GWT.create(PSHService.class);
    
    @UiField
    HTMLPanel content;
    
    @UiField
    FlowPanel title;
    
    @UiField
    FlowPanel annotace;
    
    @UiField
    FlowPanel konference;
    
    @UiField
    FlowPanel subjects;
    
    interface InvenioDocInfoUiBinder extends UiBinder<Widget, InvenioDocInfo> {
    }
    
    public InvenioDocInfo() {
        initWidget(uiBinder.createAndBindUi(this));
        
    }
    
    protected void onLoad(){
        content.setVisible(false);
    }
    
    public void clear(){
        title.clear();
        annotace.clear();
        subjects.clear();
        konference.clear();
        content.setVisible(false);
    }
    
    public void show(final String id){
        pshService.getDocVectors(id, new AsyncCallback() {
            @Override
            public void onFailure(Throwable caught) {
                clear();
                title.add(new InlineHTML("error searching nusl:"
                        + caught.getLocalizedMessage()));
            }

            @Override
            public void onSuccess(Object result) {
                clear();
                content.setVisible(true);
                SearchData js = SearchData.parseJson((String) result);
                ResultDoc doc = js.getResults().get(0);
                Anchor hl = new Anchor();
                hl.setText(doc.getTitle());
                String source = doc.getField("source");
                String sourceLink;
                if (source == null) {
                    sourceLink = "http://invenio.nusl.cz/record/" + id;
                } else if (source.equals("aleph ntk")) {
                    sourceLink = "http://aleph.techlib.cz/F/?func=direct&current_base=&format=999&doc_number=" + id;
                } else if (source.equals("invenio")) {
                    sourceLink = "http://invenio.nusl.cz/record/" + id;
                } else {
                    sourceLink = "http://invenio.nusl.cz/record/" + id;
                }
        
                hl.setHref(sourceLink);
                hl.setTarget("_blank");
                title.add(hl);
                annotace.add(new InlineHTML(doc.getField("annotace")));
                
                JsArrayString subs = doc.getFieldArray("subject");
                if(subs != null){
                    subjects.add(new InlineHTML("<b>subjects: </b>"));
                    for(int i=0; i<subs.length(); i++){
                        subjects.add(new InlineHTML(" ("+subs.get(i)+") "));
                    }
                }
                
                JsArrayString ks = doc.getFieldArray("konference");
                if(ks != null){
                    konference.add(new InlineHTML("<b>konference:</b> "));
                    for(int i=0; i<ks.length(); i++){
                        konference.add(new InlineHTML(" "+ks.get(i)+" "));
                    }
                }
            }
        });
    }
}
